package com.flowershop.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flowershop.entity.CreativeProduct;
import com.flowershop.entity.Customer;
import com.flowershop.entity.Flower;
import com.flowershop.entity.Order;
import com.flowershop.entity.OrderItem;
import com.flowershop.mapper.OrderMapper;
import com.flowershop.service.CreativeProductService;
import com.flowershop.service.CustomerService;
import com.flowershop.service.FlowerService;
import com.flowershop.service.OrderItemService;
import com.flowershop.service.OrderService;
import com.flowershop.vo.OrderItemVO;
import com.flowershop.vo.OrderListVO;
import com.flowershop.vo.OrderVO;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    
    @Autowired
    private OrderItemService orderItemService;
    
    @Autowired
    private CustomerService customerService;
    
    @Autowired
    private FlowerService flowerService;
    
    @Autowired
    private CreativeProductService creativeProductService;

    @Override
    public Page<OrderListVO> getOrderPage(Page<Order> page, String orderNo, Long customerId, String status) {
        // 构建查询条件
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        if (orderNo != null && !orderNo.isEmpty()) {
            queryWrapper.eq(Order::getOrderNo, orderNo);
        }
        if (customerId != null) {
            queryWrapper.eq(Order::getCustomerId, customerId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(Order::getStatus, status);
        }
        queryWrapper.orderByDesc(Order::getCreatedTime);

        // 查询订单数据
        Page<Order> orderPage = this.page(page, queryWrapper);

        // 转换为VO对象
        List<OrderListVO> records = orderPage.getRecords().stream().map(order -> {
            OrderListVO vo = new OrderListVO();
            BeanUtils.copyProperties(order, vo);

            // 获取客户名称
            Customer customer = customerService.getById(order.getCustomerId());
            if (customer != null) {
                vo.setCustomerName(customer.getName());
            }

            return vo;
        }).collect(Collectors.toList());

        // 构建返回结果
        Page<OrderListVO> result = new Page<>();
        result.setRecords(records);
        result.setTotal(orderPage.getTotal());
        result.setCurrent(orderPage.getCurrent());
        result.setSize(orderPage.getSize());
        
        return result;
    }
    
    @Override
    @Transactional
    public Order createOrder(Order order) {
        order.setOrderNo(generateOrderNo());
        order.setStatus("PENDING");
        this.save(order);
        return order;
    }
    
    @Override
    @Transactional
    public boolean updateOrderStatus(Long id, String status) {
        Order order = this.getById(id);
        if (order == null) {
            return false;
        }
        
        order.setStatus(status);
        return this.updateById(order);
    }
    
    @Override
    public OrderVO getOrderDetail(Long id) {
        // 获取订单基本信息
        Order order = this.getById(id);
        if (order == null) {
            return null;
        }
        
        // 转换为VO对象
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(order, orderVO);
        
        // 获取客户信息
        Customer customer = customerService.getById(order.getCustomerId());
        if (customer != null) {
            orderVO.setCustomerName(customer.getName());
        }
        
        // 获取订单项
        LambdaQueryWrapper<OrderItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderItem::getOrderId, id);
        List<OrderItem> orderItems = orderItemService.list(queryWrapper);
        
        // 转换订单项并填充商品名称
        List<OrderItemVO> itemVOs = new ArrayList<>();
        for (OrderItem item : orderItems) {
            OrderItemVO itemVO = new OrderItemVO();
            BeanUtils.copyProperties(item, itemVO);
            
            // 根据商品类型获取商品名称
            if ("FLOWER".equals(item.getProductType())) {
                Flower flower = flowerService.getById(item.getProductId());
                if (flower != null) {
                    itemVO.setName(flower.getName());
                }
            } else if ("CREATIVE".equals(item.getProductType())) {
                CreativeProduct product = creativeProductService.getById(item.getProductId());
                if (product != null) {
                    itemVO.setName(product.getName());
                }
            }
            
            itemVOs.add(itemVO);
        }
        
        orderVO.setItems(itemVOs);
        return orderVO;
    }
    
    private String generateOrderNo() {
        return UUID.randomUUID().toString().replace("-", "").substring(0, 16);
    }
} 